<form action="${ctx}/channel/manageform" method="post" id="addChannel" enctype="multipart/form-data" class="change-error-two-box">
		<input type="hidden" name="channelId" />
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>线路类型：</label>
				<label class="radio-inline"><input type="radio" name="channelType" value="1" checked >SIM网关</label>
				<label class="radio-inline"><input type="radio" name="channelType" value="2">SIP线路</label>
			</div>
			<div class="col-sm-6 sip-show <#if !channel.channelId??>hide</#if>">
				<label>批量添加：</label>
				<label class="radio-inline"><input type="radio" name="batchAdd" value="0" checked >否</label>
				<label class="radio-inline"><input type="radio" name="batchAdd" value="1">是</label>
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6 col-select179-box">
				<label><span style="color: red">*</span>网关标识：</label>
				<select class="form-control select2" name="voxGatewayName" required >
					<option value="">--请选择--</option>
					<#list gateways as a>
						<option value="${a.gwName}">${a.gwName}</option>
					</#list>
				</select>
			</div>
			<div class="col-sm-6">
				<label><span style="color: red">*</span>网关IP：</label>
				<input class="form-control" type="text" name="gatewayIp" required maxlength="50" />
			</div>
		</div>
		<div class="form-group form-inline row" id="onlyOneAdd">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>外呼号码:</label>
				<input class="form-control" type="text" name="channelName" maxlength="30" />
			</div>
			<div class="col-sm-6">
				<label><span style="color: red">*</span>IP/TEL:</label>
				<input class="form-control" type="text" name="channelNum" maxlength="30" />
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>录音地址:</label>
				<input class="form-control" type="text" name="voxServerAddr" required maxlength="200" />
			</div>
			<div class="col-sm-6">
				<label>网关api_ip:</label>
				<input class="form-control" type="text" name="gatewayApiIp" maxlength="50" />
			</div>
		</div>
		
		<div class="form-group form-inline row sim-show">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>卡槽号:</label>
				<select class="form-control" name="port" required >
					<option value="">--请选择--</option>
					<#list 1 .. 32 as i>
						<option value="${i}">${i}</option>
					</#list>
				</select>
			</div>
		</div>
		<div class="form-group form-inline row sim-show">
			<div class="col-sm-6">
				<label>SIM卡A:</label>
				<input class="form-control" type="text" name="simAPhone" placeholder="请输入外呼号码" maxlength="30" />
			</div>
			<div class="col-sm-6">
				<label>SIM卡B:</label>
				<input class="form-control" type="text" name="simBPhone" placeholder="请输入外呼号码" maxlength="30" />
			</div>
		</div>
		<div class="form-group form-inline row sim-show <#if channel.channelType?? && channel.channelType == 2>hide</#if>">
			<div class="col-sm-6">
				<label>SIM卡C:</label>
				<input class="form-control" type="text" name="simCPhone" placeholder="请输入外呼号码" maxlength="30" />
			</div>
			<div class="col-sm-6">
				<label>SIM卡D:</label>
				<input class="form-control" type="text" name="simDPhone" placeholder="请输入外呼号码" maxlength="30" />
			</div>
		</div>
		
		<#-- 下面的新增时，或SIP线路编辑时时才显示 -->
		<div class="form-group form-inline row sip-show <#if !channel.channelId??>hide</#if>">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>并发数：</label>
				<input class="form-control" type="text" name="concurrentNum" maxlength="10" required digits="true" />
			</div>
			<div class="col-sm-6">
				<label><span style="color: red">*</span>AI比例：</label>
				<select class="form-control" name="robotRatio" id="robotRatio" style="width: 120px;" required >
					<option value="">--请选择--</option>
					<#list robotRatioList as a>
						<option value="${a}">${a}</option>
					</#list>
				</select>
			</div>
		</div>
		<div class="form-group form-inline row sip-show <#if !channel.channelId??>hide</#if>" id="importAdd">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>导入号码：</label>
				<input type="file" name="fileupload" style="display: inline-block;" fileMaxSize="1024 * 1024 * 20" accept=".xls" disabled />
			</div>
			<div class="col-sm-6">
				<label>导入模板：</label>
				<a href="${ctx}/downloadFile/批量添加通道模板.xls">批量添加通道模板.xls</a>
			</div>
		</div>
		
		
		<h4 style="padding: 20px 0;border-bottom: 1px solid #e5e5e5">计费信息：</h4>
		<div class="form-group form-inline row">
			<div class="col-sm-6 company-name col-select179-box" >
				<label><span style="color: red">*</span>代理商：</label>
				<select class="form-control select2" name="agentId" id="agentId" required>
					<option value="">--请选择--</option>
					<#list agentList as a>
						<option value="${a.id}">${a.nickName}</option>
					</#list>
				</select>
				<input class="form-control" type="hidden" name="agentName" required />
			</div>
			<div class="col-sm-6 company-name col-select179-box">
				<label><span style="color: red">*</span>企业名称：</label>
				<select class="form-control select2" name="companyId" id="companyId" required>
					<option value="">--请选择--</option>
					<#list companys as a>
						<option value="${a.companyId}">${a.companyId}-${a.companyName}</option>
					</#list>
				</select>
				<input class="form-control" type="hidden" name="companyName" value="${channel.companyName!}" />
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>计费：</label>    
				<select class="form-control" name="clearingForm" required>
					<option value="">--请选择--</option>
					<#list clearingFormMap as key, value>
						<option value="${key}">${value}</option>
				   </#list>
				</select>
			</div>
			<div class="col-sm-6">
				<label><span style="color: red">*</span>使用月数：</label>
				<select class="form-control" name="monthsOfUse" required>
					<#list 1 .. 24 as i>
						<option value="${i}">${i}</option>
					</#list>
				</select>
			</div>
		</div>
		<div class="form-group form-inline row">
			<div class="col-sm-6">
				<label><span style="color: red">*</span>订单号:</label>
				<input class="form-control" type="text" name="orderNum" required />
			</div>
			<div class="col-sm-6">
				<label>是否启用：</label>
				<label class="radio-inline"><input type="radio" name="channelStatus"  value="0"> 停用</label>
				<label class="radio-inline"><input type="radio" name="channelStatus"  value="1" checked> 启用</label>
			</div>
		</div>
		<#-- <div class="form-group form-inline row">
			<div class="col-sm-6">
				<label>开始时间:</label>
				<input class="form-control" type="text" name="beginTimeStr" />
			</div>
			<div class="col-sm-6">
				<label>结束时间:</label>
				<label class="radio-inline" id="endTimeStr"></label>
			</div>
		</div> -->
</form>

<script type="text/javascript">
	$(':input[name="beginTimeStr"]').datepicker({
		autoclose: true,
		format: 'yyyy-mm-dd',
		clearBtn: true,
	    language: 'zh-CN'
	});
	
	$("#addChannel").validate({
        rules: {
        	channelName: {
        		trimRequired: true,
                unContainSpace : true
            },
        	channelNum: {
        		trimRequired: true,
                unContainSpace : true,
                validateChannelNum : true
            },
            beginTimeStr : {
            	beginTimeRequired : true
            }
        },
        messages: {
        	channelName: {
        		
        	},
        	channelNum: {
                required: "该字段不能为空"
            },
            beginTimeStr : {
            	beginTimeRequired : "启用时该字段不能为空"
            }
        }
	});
	
	// 返回true验证通过， 返回false验证不通过 
	$.validator.addMethod("validateChannelNum", function(value, element) {
		var channelId = $(":input[name='channelId']").val();
		var channelNum = $.trim($(":input[name='channelNum']").val());
		
		var verificationPassed = false;
		$.ajax({
			url : "${ctx}/channel/validateChannelNum",
			type : 'post',
			dataType : 'json',
			async : false,
			data : {"channelId" : channelId, "channelNum" : channelNum},
			success : function(data) {
				if (data.result == 'success') {
					verificationPassed = true;
				}
			}
		});
		
		return verificationPassed;
	}, "该字段不能为重复");
	
	// 返回true验证通过， 返回false验证不通过 
	$.validator.addMethod("beginTimeRequired", function(value, element) {
		var channelStatus = $(":input[name='channelStatus']:checked").val();
		// console.log("channelStatus="+channelStatus);
		var verificationPassed = true;
		if (channelStatus == 1 && value == '') {
			verificationPassed = false;
		}
		
		return verificationPassed;
	}, "启用时该字段不能为空");
	
	$('.select2').select2();
	$.fn.modal.Constructor.prototype.enforceFocus = function() {};

	
	/* 网关-录音地址 */
	var gateway = {
		<#list gateways as a>
			"${a.gwName}": "${a.voxServerAddr!}",
		</#list>
	};
	
	/* 代理商切换事件 */
	$(':input[name="agentId"]').change(function(){
		var agentId = $(this).val();
		var agentName = $(this).find("option:selected").text();
		$("input[name='agentName']").val(agentName);
		$(this).valid();
		
		/* 级联切换公司 */
		$("input[name='companyName']").val('');
		$(":input[name='companyId']").html('<option value="">--请选择--</option>');
		$.ajax({
			url : "${ctx}/company/list4agentId",
			type : 'post',
			dataType : 'json',
			async : false,
			data : {"agentId" : agentId},
			success : function(data) {
				if (data.result == 'success') {
					for (var i = 0; i < data.data.length; i++) {
						$(":input[name='companyId']").append('<option value="'+data.data[i].companyId+'">'+data.data[i].companyId+'-'+data.data[i].companyName+'</option>');
					}
				}
			}
		});
	});
	
	/* 公司的切换事件 */
	$("select[name='companyId']").change(function() {
		var companyName = $(this).find("option:selected").text();
		$("input[name='companyName']").val(companyName);
		$(this).valid();
	});
	
	/* 切换网关 */
	$(':input[name="voxGatewayName"]').change(function(){
		var key = $(this).val();
		/* 切换网关时，切换录音地址 */
		var voxServerAddr = gateway[''+key];
		$(":input[name='voxServerAddr']").val(voxServerAddr);
		$(this).valid();
	});
	
	// 切换是否批量添加时的操作  
	function changeBatchAdd(batchAdd) {
		if (batchAdd == 0 ) {
			// one add 
			$("#onlyOneAdd").show();
			$("#onlyOneAdd").removeClass("hide");
			$("#importAdd").hide();
			
			$("#onlyOneAdd :input").removeAttr("disabled");
			$("#importAdd :input").attr("disabled","disabled");
		} else {
			// import add 
			$("#onlyOneAdd").hide();
			$("#importAdd").removeClass("hide");
			$("#importAdd").show();
			
			$("#onlyOneAdd :input").attr("disabled","disabled");
			$("#importAdd :input").removeAttr("disabled");
		}
	}
	
	/* 切换线路类型 */
	$(':radio[name="channelType"]').change(function(){
		var channelType = $(this).val();
		if(channelType == 1) {
			// SIM
			$(".sim-show").show();
			$(".sip-show").hide();
			
			$(".sim-show :input").removeAttr("disabled");
			$(".sip-show :input").attr("disabled","disabled");
		} else {
			// SIP
			$(".sim-show").hide();
			$(".sip-show").removeClass("hide");
			$(".sip-show").show();
			
			$(".sim-show :input").attr("disabled","disabled");
			$(".sip-show :input").removeAttr("disabled");
			
			// 特殊处理（是否批量添加）
			var batchAdd = $(":input[name='batchAdd']:checked").val();
			changeBatchAdd(batchAdd);
		}
	});
	
	/* 切换-是否批量添加 */
	$(':radio[name="batchAdd"]').change(function(){
		var batchAdd = $(this).val();
		// console.log("batchAdd="+batchAdd);
		changeBatchAdd(batchAdd);
	});
	
	/* 计算结束时间 */
// 	$(':input[name="beginTimeStr"], :input[name="monthsOfUse"]').change(function() {
// 		var monthsOfUse = $(":input[name='monthsOfUse']").val();
// 		var beginTimeStr = $(":input[name='beginTimeStr']").val();
// 		if (monthsOfUse == '' || beginTimeStr == '') {
// 			return ;
// 		}
// 		$.ajax({
// 			url : "${ctx}/channel/calcEndTime",
// 			type : 'post',
// 			dataType : 'json',
// 			async : false,
// 			data : {"monthsOfUse" : monthsOfUse, "beginTimeStr" : beginTimeStr},
// 			success : function(data) {
// 				if (data.result == 'success') {
// 					$("#endTimeStr").text(data.data);
// 				}
// 			}
// 		});
// 	});
</script>
